﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UDataBaseWork;
using XF.Meeting.IDal;

namespace XF.Meeting.DAL
{
    public class BPMDal : BaseDAL, IBPMDal
    {/// <summary>
     /// 1)	流程发起成功，向业务系统提交创建结果
     /// </summary>
     /// <param name="strBTID">业务单据ID</param>
     /// <param name="strBOID">业务系统在接口传入的业务对象ID</param>
     /// <param name="iProcInstID">流程ID</param>
     /// <param name="eProcessInstanceResult">创建结果</param>
     /// <param name="strComment"></param>
     /// <param name="dtTime"></param>
     /// <returns></returns>
        public Task<bool> CreateResult(string strBTID, string strBOID, int iProcInstID, string bSuccess, string procURL, string strMessage)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@strBTID",strBTID),
                  new SqlParameter("@strBOID",strBOID),
                  new SqlParameter("@iProcInstID",iProcInstID),
                  new SqlParameter("@bSuccess",bSuccess),
                  new SqlParameter("@procURL",procURL),
                  new SqlParameter("@strMessage",strMessage)

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMCreateResult", out string strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spBPMCreateResult EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        /// <summary>
        /// 2)	流程审批过程中
        /// </summary>
        /// <param name="strBTID"></param>
        /// <param name="strBOID"></param>
        /// <param name="iProcInstID"></param>
        /// <param name="strStepName"></param>
        /// <param name="strApproverId"></param>
        /// <param name="eAction"></param>
        /// <param name="strComment"></param>
        /// <param name="dtTime"></param>
        /// <returns></returns>
        public Task<bool> Audit(string strBTID, string strBOID, int iProcInstID, string strStepName, string strApproverId, string eAction, string strComment, DateTime dtTime,int meetstate,int summarystate)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@strBTID",strBTID),
                  new SqlParameter("@strBOID",strBOID),
                  new SqlParameter("@iProcInstID",iProcInstID),
                  new SqlParameter("@strStepName",strStepName),
                  new SqlParameter("@strApproverId",strApproverId),
                  new SqlParameter("@eAction",eAction),
                   new SqlParameter("@strComment",strComment),
                     new SqlParameter("@dtTime",dtTime)
                    
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMAudit", out string strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spBPMCreateResult EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        /// <summary>
        /// 3)	流程审批结束
        /// </summary>
        /// <param name="strBTID"></param>
        /// <param name="strBOID"></param>
        /// <param name="bSuccess"></param>
        /// <param name="iProcInstID"></param>
        /// <param name="procURL"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public Task<bool> ApproveClose(string strBTID, string strBOID, int iProcInstID, string eProcessInstanceResult, string strComment, DateTime dtTime, int meetstate, int summarystate)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@strBTID",strBTID),
                  new SqlParameter("@strBOID",strBOID),
                  new SqlParameter("@iProcInstID",iProcInstID),
                  new SqlParameter("@eProcessInstanceResult",eProcessInstanceResult),
                  new SqlParameter("@strComment",strComment),
                  new SqlParameter("@dtTime",dtTime),
                  new SqlParameter("@meetstate",meetstate),
                  new SqlParameter("@summarystate",summarystate)

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMApproveClose", out string strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spBPMCreateResult EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }



        /// <summary>
        /// 4)	BPM审批过程中，更新业务数据
        /// </summary>
        /// <param name="strBTID"></param>
        /// <param name="strBOID"></param>
        /// <param name="iProcInstID"></param>
        /// <param name="strStepName"></param>
        /// <param name="strApproverId"></param>
        /// <param name="strXML"></param>
        /// <param name="dtTime"></param>
        /// <returns></returns>
        public Task<bool> UpdateBusinessData(string strBTID, string strBOID, int iProcInstID, string strStepName, string strApproverId, string strXML, DateTime dtTime)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@strBTID",strBTID),
                  new SqlParameter("@strBOID",strBOID),
                  new SqlParameter("@iProcInstID",iProcInstID),
                  new SqlParameter("@strStepName",strStepName),
                  new SqlParameter("@strApproverId",strApproverId),
                  new SqlParameter("@strXML",strXML),
                     new SqlParameter("@dtTime",dtTime)
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMUpdateBusinessData", out string strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spBPMUpdateBusinessData EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        public Task<bool> WriteBusinessDataToBPM(string bsid, string btid, string boid, string bsxml, string procInstID, string userid, string processUrl,int proType,int id)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@bsid",bsid),
                  new SqlParameter("@btid",btid),
                  new SqlParameter("@boid",boid),
                  new SqlParameter("@bsxml",bsxml),
                  new SqlParameter("@procInstID",procInstID),
                  new SqlParameter("@userid",userid),
                  new SqlParameter("@processUrl",processUrl),
                  new SqlParameter("@proType",@proType),
                  new SqlParameter("@Id",id)
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMWriteBusinessDataToBPM", out string strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spBPMWriteBusinessDataToBPM EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        public Task<bool> DoInvalid(string bsid, string btid, string boid, string procInstID, string userid, string invalidComment)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@bsid",bsid),
                  new SqlParameter("@btid",btid),
                  new SqlParameter("@boid",boid),
                  new SqlParameter("@procInstID",procInstID),
                  new SqlParameter("@userid",userid),
                   new SqlParameter("@invalidComment",invalidComment),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMDoInvalid", out string strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spBPMDoInvalid EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        public Task<DataSet> GetBPMCONFIG(int cInfoId,int type,out string strError)
        {
   
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@cInfoId",cInfoId),
                      new SqlParameter("@typeInt",type),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spBPMGetCONFIG", out DataSet ds, out  strError))
            {
                strError = string.Format("DBProvider spBPMGetCONFIG EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(ds);
        }
        public Task<DataTable> GetWXCONFIG( out string strError)
        {

            SqlParameter[] sqlParameters = new SqlParameter[] {
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetWxCONFIG", out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetWxCONFIG EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(ds.Tables[0]);
        }

        public Task<DataTable> GetCInfoByID(int InfoId)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@ID",InfoId),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetCInfoByID", out DataSet ds, out string strError))
            {
                strError = string.Format("DBProvider spGetCInfoByID EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult(ds.Tables[0]);
        }
        public Task<DataTable> GetCInfoByMinutesID(int InfoId)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@ID",InfoId),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetCInfoByMinutesID", out DataSet ds, out string strError))
                {
                strError = string.Format("DBProvider spGetCInfoByMinutesID EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }

            return Task.FromResult(ds.Tables[0]);
        }
    }
}
